/*
 * cpe.h
 * 
 * Copyright 2013 Raju <raju39k@gmail.com>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 * 
 */


#ifndef		_CPE_H_
#define		_CPE_H_

/*****************************************************************************
 *                            HEADER FILE INCLUSIONS                         *
 *****************************************************************************/
 
/**
 * @brief Standard Header file inclusions
 */
 #define _GNU_SOURCE         /**< for fcloseall() function */
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>         /**< for getuid() function */
 #include <sys/types.h>      /**< for getuid() function */


/**
 * @brief User Defined Header file inclusions
 */
 //#include "ixml.h"           /**< for including ixml library */

// #include "custom.h"         /**<  */
/* #include "soaparse.h"
 #include "soapcrt.h"
 #include "low.h"
 #include "secure.h"
 #include "que.h"
 #include "helpers.h"
 #include "typechk.h"*/


/*****************************************************************************
 *                               MACRO DEFINITIONS                           *
 *****************************************************************************/

/**
 * Macros for function return values
 */
 #define CPE_OK            0      /**< success return value */
 #define CPE_ERR           -1     /**< failure return value */

/**
 * General Macros for the program
 */
 #define BUFF_SIZE         120    /**< Buffer size for parsing config file */
 #define NULL_STRING       NULL   /**< Buffer size for parsing config file */

/**
 * Macros for Total Number of CPE Fault Codes
 * @todo Change if required as per new Amendment
 */
/* TODO: Change if required as per new Amendment */
 #define NUM_OF_FAULT_CODES  33

/**
 * Macros for printing the error logs into error log file
 */
 #define PRINT_ERROR(format, ...)                                    \
         fprintf (error_fp, "%s %s %s %s %d: ",                      \
            __DATE__, __TIME__, __FILE__, __func__, __LINE__);       \
         fprintf (error_fp, format, ## __VA_ARGS__ );

/**
 * Macros for Function tracing Entered and Exited
 */
 #define ENTERED             1         /**< Entered into function */
 #define EXITED              0         /**< Exited from function */

/**
 * Macros for printing the rpc call tracing to rpc trace log file
 * if where is 1  - Entered into function otherwise exited from function
 */
 #define PRINT_FUNC_TRACE(where)            \
    if (where == ENTERED)                   \
         fprintf (func_trace_fp, "%s %s %s: Entered into Function :   %-25s at "  \
            "Line: %d\n", __DATE__, __TIME__, __FILE__, __func__, __LINE__); \
    else                                    \
         fprintf (func_trace_fp, "%s %s %s: Exited from Function  :   %-25s at " \
            "Line: %d\n", __DATE__, __TIME__, __FILE__, __func__, __LINE__);

/**
 * Macros for printing the function calls tracing to function
 */
 #define PRINT_RPC_TRACE(format, ...)           \
         fprintf (rpc_trace_fp, "%s %s %s %s %d\n\n", __DATE__, __TIME__,    \
            __FILE__, __func__, __LINE__); \
         fprintf (rpc_trace_fp, format, ## __VA_ARGS__ ); \

/*****************************************************************************
 *                          TYPEDEFS, STRUCTURES and ENUMS                   *
 *****************************************************************************/

/**
 * Enum for type of RPC calls from ACS
 * @todo Add the other RPC Calls
 */
/*TODO: Add other RPC methodes */
 enum CPE_RPC_Calls
 {
       GET_RPC_METHODS = 1,
       INFORM_RESPONSE,
       SHEDULE_INFORM,
       GET_PARAMETER_NAMES,
       GET_PARAMETER_VALUES,
       SET_PARAMETER_VALUES,
       GET_PARAMETER_ATTRIBUTES,
       SET_PARAMETER_ATTRIBUTES,
       ADD_OBJECT,
       DELETE_OBJECT,
       DOWNLOAD,
       SHEDULE_DOWNLOAD,
       REBOOT,
       UPLOAD
       /* add RPC call's here */
 };

/**
 * Enum for the the Fault Codes generated by CPE
 * @todo Change if required as per new Amendment
 */
/* TODO: Change if required as per new Amendment */
 enum CPE_Fault_Codes
 {
      /* Error codes should be in the order, please refer the 
       * latest ammendment the list of error numbers for cpe */
       METHOD_NOT_SUPPORTED = 9000, 
       REQUEST_DENIED, 
       INTERNAL_ERROR, 
       INVALID_ARGUMENTS, 
       RESOURCES_EXCEEDED, 
       INVALID_PARAMETER_NAME, 
       INVALID_PARAMETER_TYPE, 
       INVALID_PARAMETER_VALUE, 
       ATTEMPT_TO_SET_NON_WRITABLE, 
       NOTIFICATION_REQUEST_REJECTED,
       FILE_TRANSFER_FAILURE,
       UPLOAD_FAILURE,
       FILE_TRANSFER_SERVER_AUTHENTICATION_FAILURE,
       FILE_TRANSFER_UNSUPPORTED_PROTOCOL,
       FTF_UNABLE_TO_JOIN_MULTICAST_GROUP,
       FTF_UNABLE_TO_CONTACT_FILE_SERVER,
       FTF_UNABLE_TO_ACCESS_FILE,
       FTF_UNABLE_TO_COMPLETE_DOWNLOAD,
       FTF_FILE_CORRUPTED_OR_UNUSABLE,
       FTF_FILE_AUTHENTICATION_FAILURE,
       FTF_UNABLE_TO_COMPLETE_WITHIN_TIME,
       CANCELATION_OF_FILE_TRANSFER_NOT_PERMITTED,
       INVALID_UUID_FORMAT,
       UNKOWN_EXECUTION_ENVIRONMENT,
       DISABLED_EXECUTION_ENVIRONMENT,
       DU_TO_EE_MISMATCH,
       DUPLICATE_DEPLOYMENT_UNIT,
       SYSTEM_RESOURCES_EXCEEDED,
       UNKNOWN_DEPLOYMENT_UNIT,
       INVALID_DEPLOYMENT_UNIT_STATE,
       INVALID_DU_UPDATE_DOWNGRADE_NOT_PERMOTTED,
       INVALID_DU_UPDATE_VERSION_NOT_SPECIFIED,
       INVALID_DU_UPDATE_VERSION_ALREADY_EXISTS
 };

/**
 * structure to store the device id details 
 * @todo Change if required as per new Amendment
 */
/* TODO: Change if required as per new Amendment */

 struct DeviceIdStruct {
       char Manufacturer[64];               /**< Manaufacturer Name */
       char OUI[6];                         /**< First 6 digits of OUI */
       char ProductClass[64];               /**< Type of Product */
       char SerialNumber[64];               /**< Serial Number of Product */
 };
 typedef struct DeviceIdStruct DeviceIdStruct_t;

/**
 * structure to store the Event details 
 * @todo Change if required as per new Amendment
 */
/* TODO: Change if required as per new Amendment */
 struct EventStruct {
       char EventCode[64];                  /**< Event Type */
       char CommandKey[32];                 /**< Command key of Event */
 };
 typedef struct EventStruct EventStruct_t;

/**
 * structure to store the List of Event details
 */
 struct EventList {
       EventStruct_t *Events;               /**< Array of Events */
       unsigned int size;                   /**< Number of Events */
 };
 typedef struct EventList EventList_t;

/**
 * structure to store the Parameter Value, Info, and Attributes details
 * @todo Change if required as per new Amendment, Change AccessList Size from 5 to requirement
 */
/* TODO: Change if required as per new Amendment, Change AccessList Size from 5 to requirement */
 struct ParameterDetailsStruct {
       char Name[256];                      /**< Parameter Name */
       char Value[1024];                    /**< Parameter Value */
       unsigned int Writable:1;             /**< Writable or Not? */
       unsigned int NotificationChange:1;   /**< Notification Change Flage (0/1) */
       unsigned int Notification:2;         /**< Notification (0/1/2) */
       unsigned int AccessListChange:1;     /**< Access List Change Flag (0/1) */
       char AccessList[5][64];              /**< List containig who has Write permission */
 };
 typedef struct ParameterDetailsStruct ParameterDetailsStruct_t;

/**
 * structure to store the list of ParameterDetails structures
 */
 struct ParameterValueList {
       ParameterDetailsStruct_t *Parameters;/**< Array of Parameters */
       unsigned int size;                   /**< Number of Parameters */
 };
 typedef struct ParameterValueList ParameterValueList_t;

/**
 * structure to store the date and time after parsing UTC Date & Time
 */
 struct DateTime {
       unsigned int year;                   /**< Year of the Date */
       unsigned int month;                  /**< Month of the Date */
       unsigned int day;                    /**< Day of the Date */
       unsigned int hour;                   /**< Hour of the Time */
       unsigned int min;                    /**< Minutes of the Time */
       unsigned int sec;                    /**< Seconds of the Time */
 };
 typedef struct DateTime DateTime_t;

/**
 * structure to hold the SOAP Header Details
 */
 struct SoapHeader {                        /**< Struct for SOAP Details */
       char *id;                            /**< Soap Id */
       short holdRequests;                  /**< HosdRequests Flag */
       short noMoreRequests;                /**< No more Request Flag */
 };
 typedef struct SoapHeader SoapHeader_t;

/**
 * structure to hold the SOAP Body Details
 */
 struct SoapMessage {
       SoapHeader_t Soap_Header;            /**< Soap Header */
       unsigned short type;                 /**< Type of soap message */
       IXML_Node *message;                  /**< SOAP message body */
 };
 typedef struct SoapMessage SoapMessage_t;

/**
 * structure to hold the SOAP Header and Body Details
 */
 struct FaultCodeStruct {
       char *String;                        /**< Fault Code Error String */
       char *Type;                          /**< Type of Fault (Server/Client) */
 };
 typedef struct FaultCodeStruct FaultCodeStruct_t;

/**
 * structure to hold the CWMP (CPE and ACS) Basic Details
 * @todo: if you want to use IPV6 increase the size of parameters
 */
/*TODO: if you want to use IPV6 increase the size of parameters */
 struct CWMPDetails {
       struct CPE {
             char url[64];                  /**< URL of CPE */
             char protocol[8];              /**< Protocol used for CPE */
             char ip[16];                   /**< IP Address of CPE */
             int port;                      /**< Port Number of CPE */
             char path[16];                 /**< path of CPE url */
             char username[16];             /**< Username of CPE */
             char password[16];             /**< Password of CPE */
             int periodic_enable;           /**< Periodic Inform Enable flag */
             int periodic_interval;         /**< Periodic Inform Interval Time */
       } cpe;
       struct ACS {
             char url[64];                  /**< URL of ACS */
             char protocol[8];              /**< Protocol used for ACS */
             char ip[16];                   /**< IP Address of ACS */
             int port;                      /**< Port Number of ACS */
             char path[16];                 /**< path of ACS url */
             char username[16];             /**< Username of ACS */
             char password[16];             /**< Password of ACS */
       } acs;
       char events[16];                  /**< array of events for a particular session */ 
 };
 typedef struct CWMPDetails CWMPDetails_t;

/*****************************************************************************
 *                              FUNCTION PROTOTYPES                          *
 *****************************************************************************/
 void cpe_exit (int ret_value);
 
 
#endif
